﻿#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include "CModelMng.h"
#include "CGlobal.h"
#include "QXmlOper.h"
#include "CSqlOper.h"
#include "Public.h"
#include "CLogOper.h"
#include "CPucStatisicsChat.h"
#include "CPubAlarmTable.h"

QVector<QWidget*>* CModelMng::getPublicCardList()
{
	QString sDevList = "";
	for (int i = 0; i < m_vecDev.size(); i++)
	{
		sDevList += m_vecDev[i]->m_sDevGuid;
		sDevList += ",";
	}
	QVector<QWidget*>* pvecWidget = new QVector<QWidget *>;
	{
		CPucStatisicsChat* pWidget = new CPucStatisicsChat(0,"");//只获取设备列表，并不需要绑定设备的操作
		QString sGuid = createGuid();
		pWidget->setProperty("row", 2);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("name", A2T("报警事件统计公共卡片"));
		pvecWidget->push_back(pWidget);
	}
	{
		CPubAlarmTable* pWidget = new CPubAlarmTable(0, "");//只获取设备列表，并不需要绑定设备的操作
		QString sGuid = createGuid();
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("name", A2T("报警事件表格公共卡片"));
		pvecWidget->push_back(pWidget);
	}
	return pvecWidget;
}

QWidget* CModelMng::getPublicCard(int nCardId, QString* psCardGuid)
{
	if (psCardGuid == nullptr)
	{
		return nullptr;
	}
	QString sGuid = *psCardGuid;
	if (sGuid == "")
	{
		sGuid = createGuid();
	}
	QWidget* pWidget = nullptr;
	switch (nCardId)
	{
	case enPubStatisticsChat:
	{
		CPucStatisicsChat* pWidget = new CPucStatisicsChat(1, sGuid);
		pWidget->setProperty("row", 2);
		pWidget->setProperty("col", 1);
		pWidget->setProperty("guid", sGuid);
		pWidget->setProperty("name", A2T("报警事件统计公共卡片"));
		break;
	}
	case enPubAlarmTable:
	{
		CPubAlarmTable* pWidget = new CPubAlarmTable(1, sGuid);
		pWidget->setProperty("row", 1);
		pWidget->setProperty("col", 2);
		pWidget->setProperty("guid", sGuid);
		pWidget->setProperty("name", A2T("报警事件表格公共卡片"));
		break;
	}
	default:
		pWidget = nullptr;
		break;
	}
	return pWidget;
}

void CModelMng::slotNewRequest(QByteArray &requestData)
{
	QString sRequest = QString::fromUtf8(requestData);
	logText(sRequest);

	//根据现场不同的服务器区分逻辑，默认0海康，1坤云
	if (g_nAIServerType == 0)
	{
		//首先先判断是否Post /Alarm请求
		if (!sRequest.contains("POST /Alarm"))
		{
			return;
		}
		//区分是json报文还是xml报文
		if (sRequest.contains("application/json"))
		{
			logText(A2T("开始解析Json"));
			extractJsonAlarm(sRequest);
			logText(A2T("解析Json结束"));
		}
		else if (sRequest.contains("application/xml"))
		{
			logText(A2T("开始解析xml"));
			extractXmlAlarm(sRequest);
			logText(A2T("解析xml结束"));
		}
	}
	else if (g_nAIServerType == 1)//坤云
	{
		//区分是json报文还是xml报文
		if (sRequest.contains("application/json"))
		{
			logText(A2T("开始解析KYJson"));
			extractKYJsonAlarm(sRequest);
			logText(A2T("解析KYJson结束"));
		}
	}
}

CModelMng::CModelMng()
{
	initCfg();//初始化xml文件
	m_bDbInit=CSqlOper::instance().initSql();

	// VLC性能优化参数
	const char* vlc_args[] = {
		"--no-xlib",
		"--avcodec-threads=4",
		"--network-caching=300",
		"--clock-synchro=0",
		"--drop-late-frames",
		"--skip-frames",
		"--no-snapshot-preview",
		"--avcodec-hw=dxva2"//启用硬件解码
	};

	// 初始化 VLC 实例
	m_pVlcInstance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);

	m_pIsApiServer = new CIsapiServer();
	connect(m_pIsApiServer, &CIsapiServer::sigNewRequest, this, &CModelMng::slotNewRequest);
	m_pIsApiServer->setNetworkParameter(g_sListernIp, g_nListernPort);
	QString sErrMsg = "";
	m_pIsApiServer->startServer(sErrMsg);
	m_pResultTh = new CExportResultTh();
	m_pResultTh->ThreadStart();
}

CModelMng::~CModelMng()
{
	if (m_pIsApiServer != nullptr)
	{
		delete m_pIsApiServer;
		m_pIsApiServer = nullptr;
	}
	if (m_pResultTh)
	{
		delete m_pResultTh;
		m_pResultTh = nullptr;
	}
	/*if (m_pLogModule != nullptr)
	{
		delete m_pLogModule;
		m_pLogModule = nullptr;
	}*/
	// 最后释放VLC实例
	if (m_pVlcInstance) {
		libvlc_release(m_pVlcInstance);
		m_pVlcInstance = nullptr;
	}
}

void CModelMng::initCfg()
{
	QString m_strSystemLogDir = QCoreApplication::applicationDirPath();
	QString filename = m_strSystemLogDir + "/DLL/HKAICfg/HKAICfg.xml";
	XmlOper::SetXmlName(filename);
	QVector<QString>vecIp;      //IsapiSever监听地址
	QVector<QString>vecPort;    //IsapiSever监听端口号
	QVector<QString>vecResultPath;//报表导出路径
	QVector<QString>vecHttpUrl;    //Http访问前缀
	QVector<QString>vecCustomName;
	QVector<QString>vecResultTime;
	QVector<QString>vecAlarmTime;
	QVector<QString>vecHKAIServerIp;
	QVector<QString>vecHKAIServerType;

	vecIp = ReadXmlS("HKAI,ISAPISERVER,ip,ip");
	vecPort = ReadXmlS("HKAI,ISAPISERVER,port,port");
	vecResultPath = ReadXmlS("HKAI,OUTPATH,path,path");
	vecHttpUrl = ReadXmlS("HKAI,HTTPURL,url,url");
	vecCustomName = ReadXmlS("HKAI,CUSTOM,name,name");
	vecResultTime = ReadXmlS("HKAI,RESULTTIME,time,time");
	vecAlarmTime = ReadXmlS("HKAI,ALARMTIME,time,time");
	vecHKAIServerIp = ReadXmlS("HKAI,AISERVER,ip,ip");
	vecHKAIServerType = ReadXmlS("HKAI,AISERVER,type,type");
	if (vecIp.size() >= 1)
	{
		g_sListernIp = vecIp[0];
	}
	if (vecPort.size() >= 1)
	{
		g_nListernPort = vecPort[0].toInt();
	}
	if (vecResultPath.size() >= 1)
	{
		g_sResultPath = vecResultPath[0];
	}
	if (vecHttpUrl.size() >= 1)
	{
		g_sHttpUrl = vecHttpUrl[0];
	}
	if (vecCustomName.size() >= 1)
	{
		g_sCustomName = vecCustomName[0];
	}
	if (vecResultTime.size() >= 1)
	{
		g_nResultTime = vecResultTime[0].toInt();
	}
	if (vecAlarmTime.size() >= 1)
	{
		g_nAlarmTime = vecAlarmTime[0].toInt();
	}
	if (vecHKAIServerIp.size() >= 1)
	{
		g_sAIServerIp = vecHKAIServerIp[0];
	}
	if (vecHKAIServerType.size() >= 1)
	{
		g_nAIServerType = vecHKAIServerType[0].toInt();
	}
}

void CModelMng::extractJsonAlarm(const QString &sRequest)
{
	int nStartindex = sRequest.indexOf("{");
	int nEndIndex = sRequest.lastIndexOf("}");
	QString sJson = sRequest.mid(nStartindex, nEndIndex - nStartindex + 1);
	QJsonDocument doc = QJsonDocument::fromJson(sJson.toUtf8());
	if (!doc.isNull())
	{
		if (doc.isObject())
		{
			QJsonObject jsonObject = doc.object();
			// 处理报警数据
			processAlarmData(jsonObject);
		}
	}
	else
	{
		logText(A2T("json解析失败"));
	}
}

void CModelMng::processAlarmData(const QJsonObject &json)
{
	AlarmData alarmData = AlarmData::fromJson(json);
	// 分析检测结果
	analyzeDetectionResults(alarmData);
}

void CModelMng::analyzeDetectionResults(const AlarmData &alarmData)
{
	HKAIDevice* pDev = nullptr;
	for (int i = 0; i < m_vecDev.size(); i++)
	{
		if (m_vecDev[i]->m_pDevMng->m_pDev->m_pDevData->m_sServerIp == alarmData.ipAddress && 
			m_vecDev[i]->m_pDevMng->m_pDev->m_pDevData->m_nChannel == alarmData.channelID)
		{
			pDev = m_vecDev[i];
			break;
		}
	}
	if (pDev == nullptr)
	{
		logText(A2T("找不到摄像头"));
		return;
	}
	else
	{
		logText(A2T("找到摄像头"));
	}
	int nEventType = 0;
	if (alarmData.aiopData.events.alertInfo.size() > 0)
	{
		//首先判断是否是皮带启停信号
		if (alarmData.sRuleName == A2T("皮带启停信号"))
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_pMutex->lock();
			pDev->m_pDevMng->m_pDev->m_pDevData->m_bBeltRun = true;
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dBeltRun = QDateTime::currentDateTime();
			pDev->m_pDevMng->m_pDev->m_pDevData->m_pMutex->unlock();
		}
		else
		{
			QMutexLocker(pDev->m_pDevMng->m_pDev->m_pDevData->m_pMutex);
			qDebug() << alarmData.MPName << endl;
			logText(alarmData.MPName);
			//是否算报警的二次处理
			if (alarmData.MPName == A2T("堆煤煤流占比"))
			{
				nEventType = enCoalFlow;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastCoalFlow.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastCoalFlow = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("皮带异物"))
			{
				nEventType = enBeltForeign;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltForeign.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltForeign = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("大块煤检测"))
			{
				nEventType = enLargeCoal;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastLargeCoal.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastLargeCoal = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("皮带跑偏算法"))
			{
				nEventType = enBeltDeviation;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltDeviation.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					logText(A2T("不满足时间,过滤"));
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltDeviation = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("安全帽反光衣"))
			{
				nEventType = enSafetyHelmet;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSafetyHelmet.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSafetyHelmet = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("烟雾火点"))
			{
				nEventType = enSmokeFire;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeFire.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeFire = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("抽烟打电话"))
			{
				nEventType = enSmokeMakeCall;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeMakeCall.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeMakeCall = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("传送带堵料"))
			{
				nEventType = enBlock;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBlock.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBlock = alarmData.dateTime;
				}
			}
			else if (alarmData.MPName == A2T("街面行为"))
			{
				nEventType = enStreet;
				if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastStreet.secsTo(alarmData.dateTime)) < g_nAlarmTime)
				{
					logText(A2T("不满足时间,过滤"));
					return;
				}
				else
				{
					pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastStreet = alarmData.dateTime;
				}
			}
			bool bFind = false;
			for (int i = 0; i < pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlgType.size(); i++)
			{
				if (nEventType == pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlgType[i])
				{
					bFind = true;
					break;
				}
			}
			if (!bFind)//该算法不处理
			{
				return;
			}
			QString sAlarmDesc = A2T("摄像头:%1,发生%2报警，报警时间:%3")
				.arg(pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName)
				.arg(alarmData.MPName)
				.arg(alarmData.dateTime.toString("yyyy/MM/dd hh:mm:ss"));
			QString sErrMsg = "";
			QString sGuid = createGuid();
			CSqlOper::instance().insertAlarmRecord(sGuid, pDev->m_pDevMng->m_sDevGuid, pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName,
				pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerIp, alarmData.channelID, alarmData.dateTime, alarmData.MPName, alarmData.eventState, alarmData.url,
				pDev->m_pDevMng->m_pDev->m_pDevData->m_sUserName, pDev->m_pDevMng->m_pDev->m_pDevData->m_sPassWord, sAlarmDesc, sErrMsg);
			SExecLog sExecLog;
			sExecLog.m_sDevName = A2T("AI算法");
			sExecLog.m_nAlarmLevel = enProcessAlarm;
			sExecLog.m_sDescriptionl = sAlarmDesc;
			sExecLog.m_sFuncName = A2T("算法服务");
			CLogOper::instance().addMsg(sExecLog);

			AlarmItem item;
			item.nAlarmLevel = 1;
			item.nIsConfirm = 0;
			item.sAlarmTime = alarmData.dateTime.toString("yyyy/MM/dd hh:mm:ss");
			item.sAlarmType = alarmData.MPName;
			item.sCamerName = pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName;
			item.sGuid = sGuid;
			item.sUrl = alarmData.url;
			pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlarm.prepend(item);
		}
	}
}

void CModelMng::extractXmlAlarm(const QString &sRequest)
{
	// 1. 提取XML部分（查找第一个'<'字符）
	int xmlStart = sRequest.indexOf("<?xml");
	if (xmlStart == -1)
	{
		xmlStart = sRequest.indexOf("<EventNotificationAlert");
	}

	if (xmlStart == -1)
	{
		qWarning() << "No XML found in HTTP data";
		return;
	}

	QString xmlData = sRequest.mid(xmlStart);
	logText(A2T("创建XML AlarmData"));
	AlarmData alarmData = AlarmData::fromXml(xmlData);
	logText(A2T("创建XML AlarmData结束"));
	// 分析检测结果
	if (!alarmData.bIsValid)
	{
		logText(A2T("创建XML失败"));
		return;
	}
	analyzeXmlDetectionResults(alarmData);
}

void CModelMng::analyzeXmlDetectionResults(const AlarmData &alarmData)
{
	HKAIDevice* pDev = nullptr;
	for (int i = 0; i < m_vecDev.size(); i++)
	{
		if (m_vecDev[i]->m_pDevMng->m_pDev->m_pDevData->m_sServerIp == alarmData.ipAddress &&
			(m_vecDev[i]->m_pDevMng->m_pDev->m_pDevData->m_nChannel == alarmData.channelID||
				m_vecDev[i]->m_pDevMng->m_pDev->m_pDevData->m_nChannelHW == alarmData.channelID))
		{
			pDev = m_vecDev[i];
		}
	}
	if (pDev == nullptr)
	{
		logText(A2T("未找到pDev"));
		return;
	}
		
	logText(A2T("找到pDev"));
	QString sEventType = "";
	QString sAlarmChild = "";
	int nEventType = 0;
	//是否算报警的二次处理
	if (alarmData.eventType == A2T("TMA") || alarmData.eventType == A2T("TMPA"))
	{
		sEventType = A2T("测温报警");
		sAlarmChild = A2T("测温报警");
		nEventType = enTemp;
		QMutexLocker(pDev->m_pDevMng->m_pDev->m_pDevData->m_pMutex);
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastTemp.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			logText(A2T("测温报警，二次处理报警时间太近"));
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastTemp = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("fielddetection") || alarmData.eventType == A2T("linedetection"))
	{
		QMutexLocker(pDev->m_pDevMng->m_pDev->m_pDevData->m_pMutex);
		nEventType = enPerimeter;
		sEventType = A2T("周界防范");
		if (alarmData.eventType == A2T("fielddetection"))
		{
			sAlarmChild = A2T("区域入侵");
		}
		else if(alarmData.eventType == A2T("linedetection"))
		{
			sAlarmChild = A2T("跨线报警");
		}
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastPerimeter.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			logText(A2T("周界防范，二次处理报警时间太近"));
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastPerimeter = alarmData.dateTime;
		}
	}
	if (sEventType == "")//没有接过的算法
	{
		logText(A2T("没有接过的算法"));
		return;
	}
	QMutexLocker(pDev->m_pDevMng->m_pDev->m_pDevData->m_pMutex);
	bool bFind = false;
	for (int i = 0; i < pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlgType.size(); i++)
	{
		if (pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlgType[i] == nEventType)
		{
			bFind = true;
			break;
		}
	}
	if (!bFind)//该算法不处理
	{
		logText(A2T("算法不处理"));
		return;
	}
	QString sAlarmDesc = A2T("摄像头:%1,发生%2报警，报警事件:%3,报警时间:%4")
		.arg(pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName)
		.arg(sEventType)
		.arg(sAlarmChild)
		.arg(alarmData.dateTime.toString("yyyy/MM/dd hh:mm:ss"));
	QString sErrMsg = "";
	QString sGuid = createGuid();
	CSqlOper::instance().insertAlarmRecord(sGuid, pDev->m_pDevMng->m_sDevGuid, pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName,
		pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerIp, alarmData.channelID, alarmData.dateTime, sEventType, alarmData.eventState, alarmData.url,
		pDev->m_pDevMng->m_pDev->m_pDevData->m_sUserName, pDev->m_pDevMng->m_pDev->m_pDevData->m_sPassWord, sAlarmDesc,sErrMsg);
	SExecLog sExecLog;
	sExecLog.m_sDevName = A2T("AI算法");
	sExecLog.m_nAlarmLevel = enProcessAlarm;
	sExecLog.m_sDescriptionl = sAlarmDesc;
	sExecLog.m_sFuncName = A2T("算法服务");
	CLogOper::instance().addMsg(sExecLog);

	AlarmItem item;
	item.nAlarmLevel = 1;
	item.nIsConfirm = 0;
	item.sAlarmTime = alarmData.dateTime.toString("yyyy/MM/dd hh:mm:ss");
	item.sAlarmType = sEventType;
	item.sCamerName = pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName;
	item.sGuid = sGuid;
	item.sUrl = alarmData.url;
	pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlarm.prepend(item);
}

void CModelMng::extractKYJsonAlarm(const QString &sRequest)
{
	int nStartindex = sRequest.indexOf("{");
	int nEndIndex = sRequest.lastIndexOf("}");
	QString sJson = sRequest.mid(nStartindex, nEndIndex - nStartindex + 1);
	QJsonDocument doc = QJsonDocument::fromJson(sJson.toUtf8());
	if (!doc.isNull())
	{
		if (doc.isObject())
		{
			QJsonObject jsonObject = doc.object();
			// 处理报警数据
			processKYAlarmData(jsonObject);
		}
	}
}

void CModelMng::processKYAlarmData(const QJsonObject &json)
{
	AlarmData alarmData = AlarmData::fromKYJson(json);
	// 分析检测结果
	analyzeKYDetectionResults(alarmData);
}

void CModelMng::analyzeKYDetectionResults(const AlarmData &alarmData)
{
	HKAIDevice* pDev = nullptr;
	for (int i = 0; i < m_vecDev.size(); i++)
	{
		if (m_vecDev[i]->m_pDevMng->m_pDev->m_pDevData->m_nChannel == alarmData.channelID)
		{
			pDev = m_vecDev[i];
		}
	}
	if (pDev == nullptr)
		return;

	int nEventType = 0;
	QString sEventName = "";
	QMutexLocker(pDev->m_pDevMng->m_pDev->m_pDevData->m_pMutex);
	//是否算报警的二次处理
	if (alarmData.eventType == A2T("109"))//传送带异物检测
	{
		nEventType = enBeltForeign;
		sEventName = A2T("皮带异物");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltForeign.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltForeign = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("132"))//传送带煤流量检测
	{
		nEventType = enCoalFlow;
		sEventName = A2T("堆煤煤流占比");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastCoalFlow.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastCoalFlow = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("141"))//传送带堵料识别
	{
		nEventType = enBlock;
		sEventName = A2T("传送带堵料");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBlock.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBlock = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("108"))//皮带跑偏
	{
		nEventType = enBeltDeviation;
		sEventName = A2T("皮带跑偏算法");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltDeviation.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltDeviation = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("152"))//抛沙滴漏
	{
		nEventType = enThrow;
		sEventName = A2T("抛洒滴漏");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltDeviation.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastBeltDeviation = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("163") || alarmData.eventType == A2T("4"))//跨皮带/行人闯入
	{
		nEventType = enPerimeter;
		sEventName = A2T("周界防范");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastPerimeter.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastPerimeter = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("20") || alarmData.eventType == A2T("21")
		|| alarmData.eventType == A2T("7"))//烟雾火焰
	{
		nEventType = enSmokeFire;
		sEventName = A2T("烟雾火点");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeFire.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeFire = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("120")|| alarmData.eventType == A2T("87") || alarmData.eventType == A2T("9"))//打电话
	{
		nEventType = enSmokeMakeCall;
		sEventName = A2T("抽烟打电话");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeMakeCall.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSmokeMakeCall = alarmData.dateTime;
		}
	}
	else if (alarmData.eventType == A2T("33") || alarmData.eventType == A2T("28"))//安全帽着装
	{
		nEventType = enSafetyHelmet;
		sEventName = A2T("安全帽反光衣");
		if (qAbs(pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSafetyHelmet.secsTo(alarmData.dateTime)) < g_nAlarmTime)
		{
			return;
		}
		else
		{
			pDev->m_pDevMng->m_pDev->m_pDevData->m_dLastSafetyHelmet = alarmData.dateTime;
		}
	}
	bool bFind = false;
	for (int i = 0; i < pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlgType.size(); i++)
	{
		if (nEventType == pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlgType[i])
		{
			bFind = true;
			break;
		}
	}
	if (!bFind)//该算法不处理
	{
		return;
	}
	QString sAlarmDesc = A2T("摄像头:%1,发生%2报警，报警时间:%3")
		.arg(pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName)
		.arg(sEventName)
		.arg(alarmData.dateTime.toString("yyyy/MM/dd hh:mm:ss"));
	QString sErrMsg = "";
	QString sGuid = createGuid();
	CSqlOper::instance().insertAlarmRecord(sGuid, pDev->m_pDevMng->m_sDevGuid, pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName,
		pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerIp, alarmData.channelID, alarmData.dateTime, sEventName, alarmData.eventState, alarmData.url,
		pDev->m_pDevMng->m_pDev->m_pDevData->m_sUserName, pDev->m_pDevMng->m_pDev->m_pDevData->m_sPassWord, sAlarmDesc, sErrMsg);
	SExecLog sExecLog;
	sExecLog.m_sDevName = A2T("AI算法");
	sExecLog.m_nAlarmLevel = enProcessAlarm;
	sExecLog.m_sDescriptionl = sAlarmDesc;
	sExecLog.m_sFuncName = A2T("算法服务");
	CLogOper::instance().addMsg(sExecLog);

	AlarmItem item;
	item.nAlarmLevel = alarmData.degree;
	item.nIsConfirm = 0;
	item.sAlarmTime = alarmData.dateTime.toString("yyyy/MM/dd hh:mm:ss");
	item.sAlarmType = sEventName;
	item.sCamerName = pDev->m_pDevMng->m_pDev->m_pDevData->m_sCamerName;
	item.sGuid = sGuid;
	item.sUrl = alarmData.url;
	pDev->m_pDevMng->m_pDev->m_pDevData->m_vecAlarm.prepend(item);
}
